package com.zhu.service.edu.controller.admin;


import com.zhu.commons.utils.R;
import com.zhu.service.edu.pojo.EduChapter;
import com.zhu.service.edu.pojo.vo.ChapterVo;
import com.zhu.service.edu.service.IEduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@RestController
@RequestMapping("admin/edu/chapter")
@Api(tags = "课程章节管理")
//@CrossOrigin
@Slf4j
public class EduChapterController {
    @Autowired
    private IEduChapterService chapterService;

    @PostMapping("save")
    @ApiOperation(value = "新增章节")
    public R save(@RequestBody EduChapter chapter){
        boolean save = chapterService.save(chapter);
        if(save){
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "根据id查询章节")
    public R getChapterById(@PathVariable String id){
        EduChapter chapter = chapterService.getById(id);
        if(chapter!=null){
            return R.ok().data("item",chapter);
        }
        return R.error().message("章节不存在！");
    }

    @PutMapping("update")
    @ApiOperation(value = "更新章节")
    public R updateChapter(@RequestBody EduChapter chapter){
        boolean b = chapterService.updateById(chapter);
        if(b){
            return R.ok().message("更新成功！");
        }
        return R.error().message("更新失败！");
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除章节")
    public R removeById(@PathVariable String id) throws Exception {
        boolean b = chapterService.deleteChapterById(id);
        if(b){
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");
    }

    @ApiOperation("查询章节及其下面的视频列表(方案一业务层解决)")
    @GetMapping("nested-list/{courseId}")
    public R nestedListByCourseId(@PathVariable String courseId){
      List<ChapterVo> chapterVoList=chapterService.nestedList(courseId);
      if(chapterVoList!=null){
          return R.ok().data("items",chapterVoList);
      }
      return  R.error().message("视频不存在！");


    }

    @ApiOperation("查询章节及其下面的视频列表(方案二sql解决 此方案章节下面如果不存在视频就不会显示)")
    @GetMapping("nested-list2/{courseId}")
    public R nested2ListByCourseId(@PathVariable String courseId){
        List<ChapterVo> chapterVoList=chapterService.nestedList2(courseId);
        if(chapterVoList!=null){
            return R.ok().data("items",chapterVoList);
        }
        return  R.error().message("视频不存在！");


    }


}
